package com.aisile.shop.controller;

import com.aisile.pojo.TbSeller;
import com.aisile.sellergoods.service.SellerService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @标题 UserServiceImpl
 * @作者 Mr.chen
 * @创建日期 2019/1/2 16:36
 * @版本号 1.0
 **/
@Service("userServiceImpl")
public class UserServiceImpl implements UserDetailsService {
    @Reference
    SellerService sellerService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        List<GrantedAuthority> grantedList = new ArrayList<>();
        //去数据库  查询对应的用户
        grantedList.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        TbSeller seller = sellerService.findById(username);
        if (seller!=null) {
            if("1".equals(seller.getStatus())){
                return new User(username,seller.getPassword(),grantedList);
            }
        }
        return null;

    }
}
